
package au.com.lastweekend.jim.controller;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.io.File;
import java.io.IOException;
import java.util.List;

import au.com.lastweekend.jim.model.ContactSheetListModel;
import au.com.lastweekend.jim.swing.CutCopyOwner;
import au.com.lastweekend.jim.swing.FileSelection;

/**
 * 
 * @author grant@lastweekend.com.au
 * @version $Id: ImageDirectoryModel.java,v 1.9 2006/02/19 06:04:54 ggardner Exp $
 */
class ContactSheetTransferable extends FileSelection implements CutCopyOwner {

    public static final DataFlavor CONTACT_SHEET_DATA_FLAVOUR = new DataFlavor(ContactSheetListModel.class,
            "ContactSheetListModel transferable");;
    private final ContactSheetListModel contactSheetListModel;

    /**
     * @param fileList
     * @param contactSheetListModel
     */
    public ContactSheetTransferable(ContactSheetListModel contactSheetListModel, List<File> fileList) {

        super(fileList);

        this.contactSheetListModel = contactSheetListModel;
    }

    @Override
    public DataFlavor[] getTransferDataFlavors() {

        if (flavours == null) {
            DataFlavor[] superFlavours = super.getTransferDataFlavors();
            flavours = new DataFlavor[superFlavours.length + 2];
            flavours[0] = CutCopyOwner.DATA_FLAVOUR;
            flavours[1] = CONTACT_SHEET_DATA_FLAVOUR;
            System.arraycopy(superFlavours, 0, flavours, 2, superFlavours.length);
        }
        return flavours;
    }

    @Override
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {

        if (CutCopyOwner.DATA_FLAVOUR.equals(flavor)) {
            return this;
        }

        if (CONTACT_SHEET_DATA_FLAVOUR.equals(flavor)) {
            return contactSheetListModel;
        }
        
        return super.getTransferData(flavor);
    }

    private int action = DnDConstants.ACTION_NONE;
    private DataFlavor[] flavours;

    /*
     * @see au.com.lastweekend.swing.CutCopyOwner#getAction()
     */
    public int getAction() {

        return action;
    }

    /*
     * @see au.com.lastweekend.swing.CutCopyOwner#setPasteAction(int)
     */
    public void setPasteAction(int action) {

        this.action = action;
        if (action == DnDConstants.ACTION_MOVE) {
            this.contactSheetListModel.setCutItems(getFiles());
        } else {
            this.contactSheetListModel.setCutItems(null);
        }
    }

    /*
     * @see au.com.lastweekend.swing.CutCopyOwner#pasteDone(boolean)
     */
    public void pasteDone(boolean actionResult) {

        this.contactSheetListModel.setCutItems(null);

    }

    /**
     * @return
     */
    public ContactSheetListModel getModel() {

        return this.contactSheetListModel;
    }

}